package com.h3c.ptability.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eos.common.constant.StatusCodeEnum;
import com.eos.common.util.Result;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.h3c.ptability.dto.PtAllWorkerViewDTO;
import com.h3c.ptability.entity.PtAllWorker;
import com.h3c.ptability.service.IPtAllWorkerService;
import com.h3c.ptability.dto.PtAllWorkerDTO;
import com.h3c.ptability.support.PageQuery;
import com.h3c.ptability.support.ResultData;
import com.h3c.ptability.utils.ReturnCodeData;
import com.h3c.ptability.utils.ReturnDataUtil;
import com.h3c.ptability.vo.PtAllWorkerVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * Copyright (C), 2022-2022, h3c
 * FileName: PtAllWorkerController
 *
 * @Author: l22886
 * @Date: 2022-03-14
 * Description: pt_all_worker controller
 * Version: 1.0
 */
@Api(tags = " PtAllWorker 接口")
@CrossOrigin
@Slf4j
@RestController
@RequestMapping(value = "/ptAllWorker", produces = "application/json;charset=utf-8")
public class PtAllWorkerController {

    @Resource
    private IPtAllWorkerService ptAllWorkerService;

    /**
     * @title: queryFuzzyListForPage
     * @description: 列表分页模糊查询
     * @params: [ paramsDTO, currentPage, pageSize ]
     * @return: org.springframework.http.ResponseEntity
     * @createTime: 2022-03-14
     * @version: 1.0
     * @author: l22886
     */
    @ApiOperation(value = "列表分页查询", notes = "列表分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "paramsDTO", value = "实体参数信息", required = true, dataType = "PtAllWorkerDTO", paramType = "query"),
            @ApiImplicitParam(name = "currentPage", value = "当前页", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "一页数据条数", required = true, dataType = "Integer", paramType = "query")
    })
    @RequestMapping(value = "/queryFuzzyListForPage", method = RequestMethod.GET)
    public Result queryFuzzyListForPage(@ModelAttribute PtAllWorkerDTO paramsDTO,
                                        @RequestParam(name = "currentPage", required = true) Integer currentPage,
                                        @RequestParam(name = "pageSize", required = true) Integer pageSize) {
        log.info("准备执行  列表分页模糊查询，传入的参数为：" + JSON.toJSONString(paramsDTO));
        //分页
        Page page = PageHelper.startPage(currentPage, pageSize);
        //执行
        List<PtAllWorkerViewDTO> resultDTOList = ptAllWorkerService.selectFuzzyByDto(paramsDTO);
        PageInfo<PtAllWorkerViewDTO> pageInfo = page.toPageInfo();
        pageInfo.setList(resultDTOList);
        log.info("执行列表分页模糊查询完成，准备返回的结果为：" + JSON.toJSONString(pageInfo));
        return new Result(true, 20000, "列表分页模糊查询成功", pageInfo);
    }

    @PostMapping("queryPtAllWorker")
    public Result<List<PtAllWorkerViewDTO>> queryPtAllWorker(@RequestBody PtAllWorkerDTO dto) {
        if (StringUtils.isEmpty(dto.getWorkerId())) {
            return new Result<>(true, StatusCodeEnum.NORMAL, new ArrayList<>());
        }
        List<PtAllWorkerViewDTO> ptAllWorkerViewDTOS = ptAllWorkerService.selectFuzzyByDto(dto);
        return new Result<>(true, StatusCodeEnum.NORMAL, ptAllWorkerViewDTOS);
    }


    /**
     * @title: save
     * @description: 数据插入
     * @params: [ paramsDTO ]
     * @return: org.springframework.http.ResponseEntity
     * @createTime: 2022-03-14
     * @version: 1.0
     * @author: l22886
     */
    @ApiOperation(value = "数据插入", notes = "数据插入")
    @ApiImplicitParams({@ApiImplicitParam(name = "paramsDTO", value = "实体参数信息", required = true, dataType = "PtAllWorker", paramType = "body")})
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result save(@RequestBody PtAllWorkerDTO paramsDTO) {
        log.info("准备执行 数据插入，传入的参数为：" + JSON.toJSONString(paramsDTO));
        //执行
        Integer result = ptAllWorkerService.insertDto(paramsDTO);
        log.info("执行 数据插入完成，准备返回的结果为：" + JSON.toJSONString(result));
        return new Result(true, 20000, "数据插入成功", result);
    }

    /**
     * @title: save
     * @description: 插入合作员工
     * @params: [ paramsDTO ]
     * @return: org.springframework.http.ResponseEntity
     * @createTime: 2022-03-14
     * @version: 1.0
     * @author: j26296
     */
    @ApiOperation(value = "数据插入", notes = "数据插入")
    @ApiImplicitParams({@ApiImplicitParam(name = "paramsDTO", value = "实体参数信息", required = true, dataType = "PtAllWorker", paramType = "body")})
    @RequestMapping(value = "/saveCooperativeWorker", method = RequestMethod.POST)
    public Result saveCooperativeWorker(@RequestBody PtAllWorkerDTO paramsDTO) {
        log.info("准备执行 数据插入，传入的参数为：" + JSON.toJSONString(paramsDTO));
        //执行
        Integer result = ptAllWorkerService.addCooperativeWorker(paramsDTO);
        log.info("执行 数据插入完成，准备返回的结果为：" + JSON.toJSONString(result));
        return new Result(true, 20000, "数据插入成功", result);
    }

    /**
     * @title: update
     * @description: 数据修改
     * @params: [ paramsDTO ]
     * @return: org.springframework.http.ResponseEntity
     * @createTime: 2022-03-14
     * @version: 1.0
     * @author: l22886
     */
    @ApiOperation(value = "数据修改", notes = "数据修改")
    @ApiImplicitParams({@ApiImplicitParam(name = "paramsDTO", value = "实体参数信息", required = true, dataType = "PtAllWorker", paramType = "body")})
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Result update(@RequestBody PtAllWorkerDTO paramsDTO) {
        log.info("准备执行 数据修改，传入的参数为：" + JSON.toJSONString(paramsDTO));
        //执行
        Integer result = ptAllWorkerService.updateDto(paramsDTO);
        log.info("执行 数据修改完成，准备返回的结果为：" + JSON.toJSONString(result));
        return new Result(true, 20000, "数据修改成功", result);
    }

    /**
     * @title: delete
     * @description: 数据删除
     * @params: [ id ]
     * @return: org.springframework.http.ResponseEntity
     * @createTime: 2022-03-14
     * @version: 1.0
     * @author: l22886
     */
    @ApiOperation(value = "数据删除", notes = "数据删除")
    @ApiImplicitParams({@ApiImplicitParam(name = "paramsDTO", value = "实体参数信息", required = true, dataType = "PtAllWorker", paramType = "body")})
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Result delete(@RequestBody PtAllWorkerDTO paramsDTO) {
        log.info("准备执行 数据删除，传入的参数为：" + JSON.toJSONString(paramsDTO));
        //执行
        Integer result = ptAllWorkerService.deleteDto(paramsDTO);
        log.info("执行 数据删除 完成，准备返回的结果为：" + JSON.toJSONString(result));
        return new Result(true, 20000, "数据删除", result);
    }

    @GetMapping("worker/{keyword}")
    public ResponseEntity<JSONObject> searchWorker(@PathVariable String keyword) {

        ResponseEntity<JSONObject> success = new ReturnDataUtil().success(ptAllWorkerService.searchWorker(keyword));
        return success;
    }

    @RequestMapping(value = "/syncAllWorker", method = RequestMethod.POST)
    public Result syncAllWorker() {
        ptAllWorkerService.syncWorker();
        return new Result(true, StatusCodeEnum.NORMAL);
    }


    /**
     * @title: importAllWorker
     * @description:
     * @params: [ paramsDTO ]
     * @return: org.springframework.http.ResponseEntity
     * @createTime: 2022-04-14
     * @version: 1.0
     * @author: j26296
     */
    @ApiOperation(value = "用户角色导入", notes = "用户角色导入")
    @ApiImplicitParams({@ApiImplicitParam(name = "file", value = "导入数据文件", required = true, dataType = "MultipartFile", paramType = "body")})
    @RequestMapping(value = "/importAllWorker", method = RequestMethod.POST)
    public ResponseEntity<JSONObject> importAllWorker(@RequestParam(value = "file") MultipartFile file) {
        Integer result = ptAllWorkerService.importAllWorker(file);
        JSONObject data = new JSONObject();
        data.put("data", result);
        data.put("msg", result + "人导入成功。");
        data.put("code", ReturnCodeData.SUCCESS);
        return new ResponseEntity<>(data, HttpStatus.ACCEPTED);
    }

    /**
     * 服务间调用获取员工信息
     *
     * @param userId  员工Id
     * @param account 员工域账号
     * @return
     */
    @GetMapping("/getUserByIdOrAccount")
    public PtAllWorker getUserByIdOrAccount(@RequestParam(required = false) String userId,
                                            @RequestParam(required = false) String account) {
        if (StringUtils.isBlank(userId) && StringUtils.isBlank(account)) {
            return null;
        }
        return ptAllWorkerService.getOne(new QueryWrapper<PtAllWorker>().lambda()
                .eq(StringUtils.isNotBlank(userId), PtAllWorker::getWorkerId, userId)
                .eq(StringUtils.isNotBlank(account), PtAllWorker::getDomainAccount, account)
        );
    }

    /**
     * 服务间调用获取员工信息
     *
     * @param email 邮箱
     * @param phone 手机号
     * @return
     */
    @GetMapping("/getUserByEmailOrPhone")
    public PtAllWorker getUserByEmailOrPhone(@RequestParam(required = false) String email,
                                             @RequestParam(required = false) String phone) {
        PtAllWorker ptAllWorker = null;
        if (StringUtils.isNotBlank(email)) {
            ptAllWorker = ptAllWorkerService.getOne(new QueryWrapper<PtAllWorker>().lambda()
                    .eq(PtAllWorker::getEmail, email)
            );
        }
        if (Objects.isNull(ptAllWorker)) {
            ptAllWorker = ptAllWorkerService.getOne(new QueryWrapper<PtAllWorker>().lambda()
                    .eq(PtAllWorker::getPhoneNo, phone)
            );
        }
        return ptAllWorker;
    }


    /**
     * 服务间调用获取员工信息
     *
     * @param account 员工域账号
     * @return
     */
    @GetMapping("/getUserByAccount")
    public Result getUserByAccount(@RequestParam String account) {
        PtAllWorker worker = ptAllWorkerService.getOne(new QueryWrapper<PtAllWorker>().lambda().eq(PtAllWorker::getDomainAccount, account));
        return new Result(true, StatusCodeEnum.NORMAL, worker);
    }


    /**
     * 列表分页查询基础人员信息
     *
     * @param pageQuery 入参
     * @param appId     租户id
     * @return 列表分页查询基础人员信息
     */
    @PostMapping(value = "/queryPage")
    public Result<Object> queryPage(@RequestBody PageQuery<PtAllWorkerDTO> pageQuery, @RequestParam String appId) {
        log.info("准备执行  列表分页模糊查询，传入的参数为：{}-{}", appId, JSON.toJSONString(pageQuery));
        if (pageQuery.getCondition() == null) {
            pageQuery.setCondition(new PtAllWorkerDTO());
        }
        IPage<PtAllWorkerVO> pageInfo = ptAllWorkerService.queryPage(pageQuery, appId);
        return new Result(true, 20000, "列表分页模糊查询成功", pageInfo);
    }


    /**
     * 批量条件根据工号查询基础人员信息
     *
     * @param workerIds 入参
     * @return 批量条件根据工号查询基础人员信息
     */
    @PostMapping(value = "/queryWorkerInfoByDomain")
    public Result<Object> queryWorkerInfoByDomain(@RequestBody List<String> workerIds) {
        log.info("准备执行  批量条件根据工号查询基础人员信息，传入的参数为：{}", JSON.toJSONString(workerIds));

        List<PtAllWorker> ptAllWorkers = ptAllWorkerService.queryWorkerInfoByDomain(workerIds);
        return new Result(true, 20000, "批量条件根据工号查询基础人员信息查询成功", ptAllWorkers);
    }

    /**
     * 查询所有正式员工
     */
    @GetMapping("/selectRegularAllWorker")
    public Result<List<PtAllWorker> > selectRegularAllWorker() {
        List<PtAllWorker> ptAllWorkers = ptAllWorkerService.list(new QueryWrapper<PtAllWorker>().lambda().eq(PtAllWorker::getWorkerType, 1));
        return new Result<>(true, 20000, "查询所有正式员工成功", ptAllWorkers);
    }
}
